﻿using Microsoft.EntityFrameworkCore;
using Rex.Assignment.Common;
using System;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;

namespace Rex.Assignment.Infrastructure.Repository
{
    public partial class BaseRepository<T> where T : class, new()
    {
        protected DbContext dbContext;

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="t">数据对象</param>
        public int Add(T t)
        {
            dbContext.Set<T>().Add(t);
            return dbContext.SaveChanges();
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="t">数据对象</param>
        public int Update(T t)
        {
            dbContext.Set<T>().Update(t);
            return dbContext.SaveChanges();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="t">数据对象</param>
        public int Delete(T t)
        {
            dbContext.Set<T>().Remove(t);
            return dbContext.SaveChanges();
        }

        /// <summary>
        /// 查询表中的所有数据
        /// </summary>
        /// <returns></returns>
        [Obsolete("会查询表中的所有数据，不推荐使用(建议采用分页【GetModelsByPage】查询)")]
        public IQueryable<T> GetModelsAll()
        {
            return dbContext.Set<T>().AsNoTracking(); // AsNoTracking：只做查询(不监听状态，可提升性能)
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <typeparam name="type">排序的字段类型</typeparam>
        /// <param name="isAsc">是否升序</param>
        /// <param name="orderByExpression">排序表达式</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns></returns>
        public IQueryable<T> GetModels<type>(bool isAsc, Expression<Func<T, type>> orderByExpression, Expression<Func<T, bool>> whereExpression)
        {
            IQueryable<T> queryable = null;
            try
            {
                if (isAsc)
                {
                    queryable = dbContext.Set<T>().Where(whereExpression).OrderBy(orderByExpression);
                }
                else
                {
                    queryable = dbContext.Set<T>().Where(whereExpression).OrderByDescending(orderByExpression);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
            return queryable;
        }

        /// <summary>
        /// 分页查询数据信息
        /// </summary>
        /// <typeparam name="type">排序的字段类型</typeparam>
        /// <param name="page">当前页</param>
        /// <param name="limit">查询的数量</param>
        /// <param name="isAsc">是否升序</param>
        /// <param name="orderByExpression">排序表达式</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns></returns>
        public PageObject<T> GetModelsByPage<type>(int page, int limit, bool isAsc, Expression<Func<T, type>> orderByExpression, Expression<Func<T, bool>> whereExpression)
        {
            PageObject<T> po = new PageObject<T>();
            try
            {
                IQueryable<T> queryable = dbContext.Set<T>().AsQueryable();
                po.Total = queryable.Where(whereExpression).Count();
                if (isAsc)
                {
                    po.Rows = queryable.Where(whereExpression).OrderBy(orderByExpression).Skip((page - 1) * limit).Take(limit).ToList();
                }
                else
                {
                    po.Rows = queryable.Where(whereExpression).OrderByDescending(orderByExpression).Skip((page - 1) * limit).Take(limit).ToList();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
            return po;
        }

        /// <summary>
        /// 根据主键ID查询
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public T GetModelsById(object id)
        {
            return dbContext.Set<T>().Find(id);
        }

    }
}
